package com.btw;

import java.util.LinkedList;
import java.util.Queue;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Class used to make a view for the game, more difficult to 
 * make in the main.xml file...
 * 
 * This class uses a separate thread to render and 
 * another thread for updating game state
 * 
 * @author TannerMeans
 */

class VSView extends GLSurfaceView implements Runnable {
	
	// Screen constants
	public static final int CONTROL_WIDTH = 100;
	
	private Updater updater;
	public Queue<MotionEvent> events = new LinkedList<MotionEvent>();
	
	// constructor
	public VSView(Context context) {
		super(context);
		updater = new Updater(this, context);
		new Thread(updater).start();
	}
	
	public void setUpdater(Updater u) {
		this.updater = u;
	}
	
	/**
	 * begins the thread
	 */
	public void run() {
		// TODO Auto-generated method stub
		Log.i("Thread", "inisde TapDatView.run()");
	}
	
	/**
	 * handles touch events from the touchscreen
	 */
	public boolean onTouchEvent(MotionEvent event) {
		//Log.i("EVENT_DOWN", "clicked: "+ event.toString());
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN ||
				action == MotionEvent.ACTION_UP) {
			synchronized (events) {
				events.add(event);
			}
		}
		
		updater.handleClick(event);
		return true;
		
		// TODO optimization: handle events here and notify single 
		//		objects that they have been clicked on   
		
	}
	
	public Queue<MotionEvent> getEvents(){
		return events;
	}
	
	public void finish() {
		Thread.currentThread().interrupt();
	}
}